package com.cx.controller;

import com.cx.common.ApiReturn;
import com.cx.pojo.Activity;
import com.cx.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("activity")
@CrossOrigin//跨域
public class ActivityController {

    @Autowired
    ActivityService activityService;

    //添加活动
    @RequestMapping("insertActivity")
    @ResponseBody
    public ApiReturn insertActivity(Activity activity){
        int rows = activityService.insert(activity);
        if (rows<1){
            return ApiReturn.error("添加失败");
        }
        return  ApiReturn.success("添加成功");
    }

    //修改活动
    @RequestMapping("updateActivity")
    @ResponseBody
    public ApiReturn updateActivity(Activity activity){
        int rows = activityService.updateByPrimaryKey(activity);
        if (rows<1){
            return ApiReturn.error("修改失败");
        }
        return ApiReturn.success("修改成功");
    }

    //删除活动
    @RequestMapping("deleteActivity")
    @ResponseBody
    public ApiReturn deleteActivity(Integer actId) {
        int rows = activityService.deleteByPrimaryKey(actId);
        if (rows < 1) {
            return ApiReturn.error("删除失败");
        }
        return ApiReturn.success("删除成功");
    }

    //根据id查询活动
    @RequestMapping("selectActivityById")
    @ResponseBody
    public ApiReturn selectActivityById(Integer actId) {
        Activity activityInfo = activityService.selectByPrimaryKey(actId);
        if (activityInfo != null) {
            return ApiReturn.success("活动数据详情",activityInfo);
        }
        return ApiReturn.error("未获取该活动数据详情");
    }
}
